import config from "@/config"; // 导入配置文件


// 基本路径
const baseURL = config.baseURL;

// 立即报修/保养
export function submitRepairInformation(data) {
    if (data.machineModelNumber === "") {
        uni.showToast({
            duration: 2000,
            title: "报修失败,请先绑定设备",
            icon: 'none'
        })
        return;
    }
    const token = uni.getStorageSync('token');
    uni.request({
        url: `${baseURL}/order/insert`,
        method: "POST",
        data,
        header: {
            "token": token
        },
        success(res) {
            if (res.data.code == 200) {
                uni.showToast({
                    duration: 2000,
                    title: "报修成功",
                    image: '/static/image/icon/success.png',
                })

                setTimeout(function() {
                    uni.reLaunch({
                        url: '/pages/index/index'
                    })
                }, 2000)


            } else if (res.data === 'token illegal') {
                uni.showToast({
                    title: '登录过期，请重新登录',
                    icon: 'error',
                    success: function() {
                        uni.redirectTo({
                            url: '/pages/login/login'
                        })
                    }
                })
            } else {
                uni.showToast({
                    duration: 2000,
                    title: "报修失败",
                    image: '/static/image/icon/fail.png',
                })
            }
        },
        fail(err) {
            console.log("服务器罢工了");
        },
    });
}


// 获取所有故障类型
export function getAllFaultTypes() {
    return new Promise((resolve, reject) => {
        const token = uni.getStorageSync('token');
        uni.request({
            url: `${baseURL}/faultType/1`,
            method: 'GET',
            header: {
                "token": token
            },
            success(res) {
                if (res.data.code === 200) {
                    const faultTypes = res.data.data.records.map(item => item.name);
                    resolve(faultTypes);
                } else if (res.data === 'token illegal') {
                    uni.showToast({
                        title: '登录过期，请重新登录',
                        icon: 'error',
                        success: function() {
                            uni.redirectTo({
                                url: '/pages/login/login'
                            })
                        }
                    })
                } else {
                    reject("获取数据失败");
                }
            },
            fail(err) {
                reject("服务器罢工了");
            }
        });
    });
}

// 获取所有设备型号
export function getAllMachineModels() {
    return new Promise((resolve, reject) => {
        const token = uni.getStorageSync('token');
        uni.request({
            url: `${baseURL}/userMachine/list`,
            method: 'GET',
            header: {
                "token": token
            },
            data: {},
            success(res) {
                if (res.data.code === 200) {
                    const machineModels = res.data.data.map(item => item.modelNumber);
                    resolve(machineModels);
                } else if (res.data === 'token illegal') {
                    uni.showToast({
                        title: '登录过期，请重新登录',
                        icon: 'error',
                        success: function() {
                            uni.redirectTo({
                                url: '/pages/login/login'
                            })
                        }
                    })
                } else {
                    reject("请求失败");
                }
            },
            fail(err) {
                reject("服务器罢工了");
            }
        });
    });
}
